<template>
  <div class="department-container">
    <el-tree :data="data" :props="defaultProps" :expand-on-click-node="false" default-expand-all>
      <template v-slot="{data}">
        <el-row style="width:100%">
          <el-col :span="20"><div @click="text(data)">{{ data.name }}</div></el-col>
          <el-col :span="4">
            <div class="rigth-ele">
              <span class="re-name">{{ data.managerName }}</span>
              <!-- 碰到不好写的组件，复杂的组件，一定是去看element-ui，或者去网上搜 vue 下拉组件 -->
              <el-dropdown class="re-drop" @command="dropdownTmp($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </el-col>
        </el-row>
      </template>

    </el-tree>
    <!-- 这里不放具体的弹出框组件，而是放置一个封装过后的组件 -->
    <AddEditDialog
      ref="dialogRef"
      :is-edit.sync="isEdit"
      :current-id="currentId"
      :show-dialog.sync="showDialog"
      :department-list="departmentList"
      @add-success="companyDepartment"
    />
  </div>
</template>

<script>
// 获取部门列表
import { companyDepartmentRequest, delCompanyDepartment } from '@/api/department'
import { transformListToTree } from '@/utils'
// 导入自定义组件
import AddEditDialog from '@/views/department/components/addEditDialog.vue'
export default {
  name: 'Department',
  components: { AddEditDialog },
  data() {
    return {
      data: [],
      departmentList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentId: '',
      isEdit: false
    }
  },

  created() {
    // 不要这created入口函数里面，写太多的逻辑代码，一般是放在methods里面去写函数
    // 然后再在created里面调用
    this.companyDepartment()
  },
  methods: {
    async companyDepartment() {
      const res = await companyDepartmentRequest()
      // 下面这个是数据平铺的数据
      this.departmentList = res.data
      // 下面这个数据是 符合tree组件的  树形结构数据
      this.data = transformListToTree(res.data, 0)
      // console.log(res)
      this.data = res.data
    },
    dropdownTmp(type, id) {
      this.currentId = id
      console.log(id)
      if (type === 'add') {
        // 打开弹框， 但是变量又在弹窗里面，怎么办？
        this.isEdit = false
        this.showDialog = true
      } else if (type === 'delete') {
        // 删除
        this.$confirm('请确认是否要删除这个部门?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delCompanyDepartment(id)
          // 删除成功以后，要刷新部门列表（这个刷新就是调用接口，不是浏览器的刷新）
          this.companyDepartment()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        // 编辑
        this.isEdit = true
        this.showDialog = true
        // console.log('进入编辑', updateDepartmentRequest)
        // 属性监听
        // 父组件能否访问子组件
        this.$nextTick(() => {
          // this.$refs.dialogRef.getDepartmentDetail()
        })
      }
    },
    text(data) {
      console.log(data)
    }
  }
}
</script>

<style lang="scss" scope>
// calc是一个css函数，可以动态的计算一个元素的宽度和高度
// width:calc(100% - 100px)
// width:calc(100vh - 50px)
.department-container{
  margin: 30px 50px;
  .rigth-ele {
    .re-name {
      margin-right: 10px;
    }
    .re-drop{}
  }
}
</style>
